package com.luis.service.impl;

import com.luis.dao.CreditorInfoMapper;
import com.luis.model.CreditorInfo;
import com.luis.service.CreditorInfoServer;
import com.luis.utils.FastDFSUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Luis
 * @date: 2022/4/12 17:05
 * @description:
 */
@Service
public class CreditorInfoServiceImpl implements CreditorInfoServer {

    @Resource
    private CreditorInfoMapper creditorInfoMapper;

    @Override
    public List<CreditorInfo> queryAll() {
        List<CreditorInfo> list = creditorInfoMapper.selectAll();
        return list;
    }

    @Override
    public CreditorInfo findById(Integer id) {
        CreditorInfo creditorInfo = creditorInfoMapper.selectByPrimaryKey(id);
        return creditorInfo;
    }

    @Override
    public int updateFileInfo(CreditorInfo creditorInfo) {
        int ret = creditorInfoMapper.updateByPrimaryKeySelective(creditorInfo);
        return ret;
    }

    @Override
    public void deleteFileById(Integer id) {
        CreditorInfo creditorInfo = creditorInfoMapper.selectByPrimaryKey(id);
        String groupName = creditorInfo.getGroupName();
        String remoteFilePath = creditorInfo.getRemoteFilePath();

        // 先删除远程文件
        FastDFSUtil.delete(groupName, remoteFilePath);

        // 再修改数据库文件数据
        creditorInfo.setGroupName("");
        creditorInfo.setRemoteFilePath("");
        creditorInfo.setOldFileName("");
        creditorInfo.setFileSize(0L);
        creditorInfo.setFileType("");
        creditorInfoMapper.updateByPrimaryKeySelective(creditorInfo);
    }
}
